package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.JurisdictionDto;
import xyz.zhouzhaodong.crm.entity.Jurisdiction;
import xyz.zhouzhaodong.crm.mapper.JurisdictionMapper;
import xyz.zhouzhaodong.crm.service.IJurisdictionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.ExceptionEnum;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-14
 */
@Service
public class JurisdictionServiceImpl extends ServiceImpl<JurisdictionMapper, Jurisdiction> implements IJurisdictionService {

    @Resource
    JurisdictionMapper jurisdictionMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addJurisdiction(JurisdictionDto jurisdictionDto) {
        try {
            // 先判断是否重复
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            Jurisdiction jurisdiction = new Jurisdiction();
            jurisdiction.setStationId(jurisdictionDto.getStationId());
            jurisdiction.setModuleName(jurisdictionDto.getModuleName());
            jurisdiction.setModuleImage(jurisdictionDto.getModuleImage());
            jurisdiction.setPromis(jurisdictionDto.getPromis());
            RestMessage restMessage = selectJurisdiction(jurisdictionDto.getStationId(), jurisdictionDto.getModuleName());
            if (restMessage.getCode() != 0) {
                // 这里进行更新操作
                Jurisdiction j = (Jurisdiction) restMessage.getData();
                id = j.getId();
                jurisdiction.setId(id);
                jurisdiction.setUpdateUser(jurisdictionDto.getStaffId());
                jurisdiction.setUpdateTime(new Date());
                jurisdictionMapper.updateById(jurisdiction);
            } else {
                jurisdiction.setId(id);
                jurisdiction.setCreateUser(jurisdictionDto.getStaffId());
                jurisdiction.setCreateTime(new Date());
                jurisdictionMapper.insert(jurisdiction);
            }
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteJurisdiction(String id, String staffId) {
        try {
            Jurisdiction jurisdiction = new Jurisdiction();
            jurisdiction.setId(id);
            jurisdiction.setUpdateUser(staffId);
            jurisdiction.setUpdateTime(new Date());
            jurisdiction.setFlag(1);
            jurisdictionMapper.updateById(jurisdiction);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateJurisdiction(JurisdictionDto jurisdictionDto) {
        try {
            // 先判断是否重复
            RestMessage restMessage = selectJurisdiction(jurisdictionDto.getStationId(), jurisdictionDto.getModuleName());
            if (restMessage.getCode() != 0) {
                return restMessage;
            }
            Jurisdiction jurisdiction = new Jurisdiction();
            jurisdiction.setId(jurisdictionDto.getId());
            jurisdiction.setStationId(jurisdictionDto.getStationId());
            jurisdiction.setModuleName(jurisdictionDto.getModuleName());
            jurisdiction.setModuleImage(jurisdictionDto.getModuleImage());
            jurisdiction.setPromis(jurisdictionDto.getPromis());
            jurisdiction.setUpdateUser(jurisdictionDto.getStaffId());
            jurisdiction.setUpdateTime(new Date());
            jurisdictionMapper.updateById(jurisdiction);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectOne(String stationId) {
        QueryWrapper<Jurisdiction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id", stationId);
        queryWrapper.eq("flag", 0);
        List<Jurisdiction> list = jurisdictionMapper.selectList(queryWrapper);
        return RestMessage.success(list);
    }

    /**
     * 查询是否存在该岗位
     *
     * @param stationId
     * @return
     */
    public RestMessage selectJurisdiction(String stationId, String moduleName) {
        QueryWrapper<Jurisdiction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id", stationId);
        queryWrapper.eq("module_name", moduleName);
        queryWrapper.eq("flag", 0);
        List<Jurisdiction> list = jurisdictionMapper.selectList(queryWrapper);
        if (list.size() > 0 && list.get(0) != null) {
            return new RestMessage(ExceptionEnum.JURISDICTION_NAME.getCode(), ExceptionEnum.JURISDICTION_NAME.getMsg(), list.get(0));
        }
        return RestMessage.success();
    }
}
